#include "BitmapFont.h"
#include "CSGD_TextureManager.h"
#include <ctype.h>

CBitmapFont::CBitmapFont(void)
{
	m_nImageID			= -1;
	m_nCharWidth		= 0;
	m_nCharHeight		= 0;
	m_nNumCols			= 0;
	m_nNumRows			= 0;
	m_bOnlyUppercase	= false;
	m_cFirstChar		= 0;
}


CBitmapFont::~CBitmapFont(void)
{

}

void CBitmapFont::Initialize()
{
	m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/font.png"), D3DCOLOR_ARGB(255,0,0,0) );

	
	m_nCharWidth		= 22;
	m_nCharHeight		= 28;
	m_nNumCols			= 14;
	m_nNumRows			= 7;
	
	m_bOnlyUppercase	= false;
	m_cFirstChar		= _T(' ');	
}

void CBitmapFont::Shutdown(void)
{
	CSGD_TextureManager::GetInstance()->UnloadTexture( m_nImageID );
	m_nImageID = -1;
}

void CBitmapFont::Draw( const TCHAR* strOutput, int nX, int nY,
		float fScale, DWORD dwColor ) const
{
	int nColStart = nX;

	for( int i = 0; strOutput[ i ]; i++ )
	{

		TCHAR ch = strOutput[ i ];
		
		if( ch == _T(' ') )
		{
			nX += (int)((m_nCharWidth) * fScale);
			continue;
		}
		else if( ch == _T('\n') )
		{
			nY += (int)((m_nCharHeight) * fScale);
			nX =  nColStart;
			continue;
		}
		else if( ch == _T('\t') )
		{
			int curr = (int)((nX - nColStart) / (m_nCharHeight * fScale));
			int spaces = 4 - (curr % 4);
			nX += (int)(spaces * m_nCharWidth * fScale);
			continue;
		}


		if( m_bOnlyUppercase == true )
			ch = toupper( ch );

		int id = ch - m_cFirstChar;
		RECT rCell = GetRect( id );

		CSGD_TextureManager::GetInstance()->Draw(
			m_nImageID, nX, nY, 
			fScale, fScale, &rCell,
			0.0f, 0.0f, 0.0f,
			dwColor );

		int SpaceDiff = 0;
		
		for (unsigned int i = 0; i < m_LetterManager.size(); i++)
		{
			if (m_LetterManager[i].m_Letter == ch)
			{
				SpaceDiff =  m_LetterManager[i].m_SpaceDiff;
				break;
			}
		}

		nX += (int)((m_nCharWidth-SpaceDiff) * fScale);

	}
}

RECT CBitmapFont::GetRect(int id) const
{
	RECT rCell = { };

	rCell.left		= (id % m_nNumCols) * m_nCharWidth;
	rCell.top		= (id / m_nNumCols) * m_nCharHeight;
	rCell.right		= rCell.left + m_nCharWidth;
	rCell.bottom	= rCell.top  + m_nCharHeight;

	return rCell;
}

void CBitmapFont::AddLetterDiff(char letter, int space)
{
	LetterDiff Diff;

	Diff.m_Letter = letter;
	Diff.m_SpaceDiff = space;

	m_LetterManager.push_back(Diff);
}

int CBitmapFont::GetStringWidth(string str, float scale)
{
	if ( str == "")
		return 0;

	int Width = 0;

	for (int i = 0; str[i]; i++)
	{
		if (str[i] == 'A' || str[i] == 'U' ||str[i] == 'X' ||str[i] == 'a' ||
			str[i] == 'g' || str[i] == 'o' || str[i] == 'q')
				Width += (int)((m_nCharWidth - 2) * scale);

		else if (str[i] == 'B' || str[i] == 'C' ||str[i] == 'D' ||str[i] == 'E' ||
				 str[i] == 'K' || str[i] == 'N' || str[i] == 'P'|| str[i] == 'R' || 
				 str[i] == 'S'|| str[i] == 'V' || str[i] == 'Y'|| str[i] == 'b' || 
				 str[i] == 'd'|| str[i] == 'e' || str[i] == 'k'|| str[i] == 'p' || 
				 str[i] == 'u'|| str[i] == 'v' )
					Width += (int)((m_nCharWidth - 4) * scale);

		else if (str[i] == 'H' || str[i] == 'L' ||str[i] == 'T' ||str[i] == 'Z' ||
				 str[i] == 'c' || str[i] == 'h' || str[i] == 'n'|| str[i] == 'x' || 
				 str[i] == 'z')
					Width += (int)((m_nCharWidth - 6) * scale);

		else if (str[i] == 'F' || str[i] == 'J' ||str[i] == 'f' ||str[i] == 'j' ||
				 str[i] == 't' )
					Width += (int)((m_nCharWidth - 8) * scale);

		else if (str[i] == 'l' || str[i] == 'r' || str[i] == '.')
					Width += (int)((m_nCharWidth - 10) * scale);

		else if (str[i] == 'I' || str[i] == 'i' )
					Width += (int)((m_nCharWidth - 12) * scale);
		
		else if (str[i] == 'G' || str[i] == 'W' || str[i] == 'w' )
					Width += (int)((m_nCharWidth + 2) * scale);

		else
			Width += (int)(m_nCharWidth * scale);
	}

	return Width;
}